<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路②：贪心。一般来说对于超出范围的大数，会转化为字符串处理
            从右向左遍历字符串，如果str[i-1]>str[i] 就让str[i-1]-1  右边的全变为9
            */
            var monotoneIncreasingDigits = function (n) {
                let index = -1
                let str = (n + '').split('')
                for (let i = str.length - 1; i >= 1; i--) {
                    if (str[i - 1] > str[i]) {
                        str[i - 1] = (Number(str[i - 1]) - 1).toString()
                        index = i
                    }
                }
                if (index != -1) {
                    for (let i = index; i < str.length; i++) {
                        str[i] = '9'
                    }
                    str = str.join('')
                    return Number(str)
                } else {
                    str = str.join('')
                    return Number(str)
                }
            }
            monotoneIncreasingDigits(294)
        </script>
    </body>
</html>
